﻿using Autodesk.AutoCAD.Runtime;
using System;
using System.IO;
using System.Reflection;

[assembly: ExtensionApplication(typeof(NFox.Pycad.Loader))]

namespace NFox.Pycad
{
    public class Loader : IExtensionApplication
    {

        DirectoryInfo _updatedir;
        DirectoryInfo _libdir;

        public void Initialize()
        {

            //查找是否有更新版本
            var dir = DirectoryEx.CurrDirectory;
            _updatedir = dir.GetDirectory("Update");
            _libdir = dir.GetDirectory("Lib");

            //不更新本程序集
            var dll = _updatedir.GetFile("NFox.Pycad.dll");
            if (dll != null) dll.Delete();

            //更新根程序集和更新程序集
            UpdateFile("NFox.Pycad.Root.dll");
            UpdateFile("NFox.Pycad.Update.dll");

            string name = "NFox.Pycad.Root";
            var file = _libdir.GetFile($"{name}.dll");
            var ass = Assembly.LoadFrom(file.FullName);
            var t = ass.GetType($"{name}.Application");
            dynamic app = Activator.CreateInstance(t);

        }

        public void Terminate() { }

        private void UpdateFile(string name)
        {
            var dll = _updatedir.GetFile(name);
            if (dll != null)
            {
                dll.CopyTo($"{_libdir}\\{name}");
                dll.Delete();
            }
        }

    }
}
